
package cqrtplm.controller;

import com.ty.basic.entity.PrincipalEntity;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.MaterialPermissionDTO;
import cqrtplm.entity.RtZzdhDO;
import cqrtplm.service.RtZzdhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(RouteConstant.ZZDH)
//    public static final String ZZDH = "rest/Zzdh/";
public class RtZzdhController {

    @Autowired
    private RtZzdhService rtZzdhService;

    @PostMapping
    public int save(@RequestBody RtZzdhDO record) {
        return rtZzdhService.save(record);
    }

    @PutMapping
    public int update(@RequestBody RtZzdhDO record) {
        return rtZzdhService.update(record);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable String id) {
        return rtZzdhService.delete(id);
    }

    @GetMapping("/{id}")
    public RtZzdhDO getById(@PathVariable String id) {
        return rtZzdhService.getById(id);
    }

    @GetMapping
    public List<RtZzdhDO> listAll() {
        return rtZzdhService.listAll();
    }

    @GetMapping("/group/{group}")
    public List<RtZzdhDO> listByGroup(@PathVariable String group) {
        return rtZzdhService.listByGroup(group);
    }
    /**
     * 检查物料号权限
     * @param materialNumbers 物料号列表
     * @return 物料号+权限布尔值列表
     */
    @PostMapping("/check-permissions")
    public List<MaterialPermissionDTO> checkMaterialPermissions(@RequestBody List<String> materialNumbers) {
        PrincipalEntity userDO = TyAccountContext.getUser();
        return materialNumbers.stream()
                .map(material -> new MaterialPermissionDTO(material, rtZzdhService.hasPermission(material,userDO)))
                .collect(Collectors.toList());
    }
}
